package com.fhj.movie.activity;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.fhj.movie.entity.HotListEntity;
import com.fhj.movie.entity.HyyTeJiaHuoDongEntity;
import com.fhj.movie.entity.RecListEntity;
import com.fhj.movie.interfaces.CallbackInterface;
import com.fhj.movie.view.MyListView;
import com.fhj.movie_fhj_01.R;
import com.fhj.utils.CacheUtils;
import com.fhj.utils.HttpConnectionUtils;
import com.fhj.utils.JsonUtils;
import com.lidroid.xutils.BitmapUtils;

public class HyyTeJiaHuoDong extends Activity {
	private RequestQueue requestQueue;

	private CacheUtils cache;
	
	private List<HotListEntity> hotListEntities;
	
	private List<RecListEntity> recListEntities;
	
	private HyyTeJiaHuoDongEntity entity;
	
	private MyHotAdapter hotAdapter;
	
	private MyRecAdapter recAdapter;
	
	private MyListView hotListView;
	
	private MyListView recListView;
	
	private BitmapUtils bitmapUtils;
	
	private ImageView imageView1;
	
	private TextView textView1;
	
	private Animation loadAnimation;
	
	private LinearLayout layout1,layout2;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tejiahuodong);
		hotListView=(MyListView) findViewById(R.id.hotcontent);
		recListView=(MyListView) findViewById(R.id.recontent);
		imageView1=(ImageView)findViewById(R.id.TejiahuodongXuanzhuan);
		textView1=(TextView)findViewById(R.id.tejiahuodongwenzi);
		layout1=(LinearLayout)findViewById(R.id.hoet_huo_dong);
		layout2=(LinearLayout)findViewById(R.id.rec_huo_dong);
		
		loadAnimation = AnimationUtils.loadAnimation(this, R.anim.taste);
		LinearInterpolator  lir=new LinearInterpolator();
		loadAnimation.setInterpolator(lir);
		
		hotAdapter=new MyHotAdapter();
		recAdapter=new MyRecAdapter();
		bitmapUtils=new BitmapUtils(this);
		hotListEntities=new ArrayList<HotListEntity>();
		recListEntities=new ArrayList<RecListEntity>();
		requestQueue=Volley.newRequestQueue(this);
		imageView1.startAnimation(loadAnimation);
		HttpConnectionUtils.getActivityOptionData(this,2,440300,
				new CallbackInterface() {

					@Override
					public void JsonCallback(JSONObject object) {
						// TODO Auto-generated method stub
						layout1.setVisibility(View.VISIBLE);
						layout2.setVisibility(View.VISIBLE);
						imageView1.clearAnimation();
						
						imageView1.setVisibility(View.INVISIBLE);
						textView1.setVisibility(View.INVISIBLE);
						entity=JsonUtils.parseHuoDongData(object);
						hotListEntities=entity.getHotListEntities();
						hotAdapter.notifyDataSetChanged();
						recListEntities=entity.getRecListEntities();
						recAdapter.notifyDataSetChanged();
					}});
		
		hotListView.setAdapter(hotAdapter);
		recListView.setAdapter(recAdapter);
//		setPullLvHeight(hotListView);
//		setPullLvHeight(recListView);
		hotListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(HyyTeJiaHuoDong.this,HyyItemClick.class);
				intent.putExtra("title", hotListEntities.get(position).getTitle());
				intent.putExtra("clickUrl", hotListEntities.get(position).getClickUrl());
				startActivity(intent);
			}
		});
		recListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(HyyTeJiaHuoDong.this,HyyItemClick.class);
				intent.putExtra("title", recListEntities.get(position).getTitle());
				intent.putExtra("clickUrl", recListEntities.get(position).getClickUrl());
				startActivity(intent);
			}
		});
	}
	class MyHotAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return hotListEntities.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return hotListEntities.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-gehonerated method stub
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = LayoutInflater.from(HyyTeJiaHuoDong.this).inflate(
						R.layout.fragment_lhuo_dong_list_tem, parent, false);
				holder.item_image = (ImageView) convertView
						.findViewById(R.id.item_image);
				holder.item_title = (TextView) convertView
						.findViewById(R.id.item_title);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			String url = hotListEntities.get(position).getPicUrl();
			// TODO 下载图片
			holder.item_image.setImageResource(R.drawable.place_holder_h);
			// TODO 下载图片
			if (url != null) {
				bitmapUtils.display(holder.item_image, url);
			}
			holder.item_title.setText(hotListEntities.get(position).getTitle());
			
			
			return convertView;
		}

	}

	class MyRecAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return recListEntities.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return recListEntities.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-gehonerated method stub
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = LayoutInflater.from(HyyTeJiaHuoDong.this).inflate(
						R.layout.fragment_lhuo_dong_list_tem, parent, false);
				holder.item_image = (ImageView) convertView
						.findViewById(R.id.item_image);
				holder.item_title = (TextView) convertView
						.findViewById(R.id.item_title);
				holder.guanShang = (ImageView) convertView
						.findViewById(R.id.guanshang);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			String url = recListEntities.get(position).getPicUrl();
			// TODO 下载图片
			holder.item_image.setImageResource(R.drawable.place_holder_h);
			// TODO 下载图片
			if (url != null) {
				bitmapUtils.display(holder.item_image, url);
			}
			holder.item_title.setText(recListEntities.get(position).getTitle());
			
				holder.guanShang.setImageDrawable(getResources().getDrawable(R.drawable.mark_sale_end));
			
			return convertView;
		}

	}
	class ViewHolder {
		public ImageView item_image;
		public TextView item_title;
		public ImageView guanShang;
	}
	private void setPullLvHeight(ListView listView){  
		
		        ListAdapter listAdapter = listView.getAdapter();   
		        if (listAdapter == null) {  
		            // pre-condition  
		            return;  
		        }  
		  
		        int totalHeight = 0;  
		        for (int i = 0; i < listAdapter.getCount(); i++) {  
		            View listItem = listAdapter.getView(i, null, listView);  
		            listItem.measure(0, 0);  
		            totalHeight += listItem.getMeasuredHeight();  
		        }  
		  
		        ViewGroup.LayoutParams params = listView.getLayoutParams();  
		        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));  
		        listView.setLayoutParams(params); 
    }  

}
